package org.jeecg.modules.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hc.entity.*;
import org.jeecg.modules.hc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 企业信息
 * @Author: jeecg-boot
 * @Date:   2023-07-04
 * @Version: V1.0
 */
@Api(tags="企业信息")
@RestController
@RequestMapping("/hc/hcCompanyInfo")
@Slf4j
public class HcCompanyInfoController extends JeecgController<HcCompanyInfo, IHcCompanyInfoService> {
	@Autowired
	private IHcCompanyInfoService hcCompanyInfoService;

	@Autowired
	private IHcCompanyPickupAddressService companyPickupAddressService;
	@Autowired
	private IHcCompanyDepartmentService companyDepartmentService;
	@Autowired
	private IHcCompanyBannerService companyBannerService;
	@Autowired
	private IHcSiteService hcSiteService;


	/**
	 * 获取所有企业信息
	 */
	@ApiOperation(value="获取所有企业信息", notes="获取所有企业信息")
	@GetMapping(value = "/getAllCompanyList")
	public Result<?> getAllCompanyList() {
		return Result.OK(hcCompanyInfoService.getAllCompanyList());
	}



	/**
	 * 分页列表查询
	 *
	 * @param hcCompanyInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="企业信息-分页列表查询", notes="企业信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<HcCompanyInfo>> queryPageList(HcCompanyInfo hcCompanyInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HcCompanyInfo> queryWrapper = QueryGenerator.initQueryWrapper(hcCompanyInfo, req.getParameterMap());
		Page<HcCompanyInfo> page = new Page<HcCompanyInfo>(pageNo, pageSize);
		IPage<HcCompanyInfo> pageList = hcCompanyInfoService.page(page, queryWrapper);
		List<HcCompanyInfo> records = pageList.getRecords();
		for (HcCompanyInfo record : records) {
			//站点
			LambdaQueryWrapper<HcSite> siteLambdaQueryWrapper = new LambdaQueryWrapper<>();
			siteLambdaQueryWrapper.eq(HcSite::getId,record.getSiteId());
			HcSite hcSite = hcSiteService.getOne(siteLambdaQueryWrapper);
			record.setSiteName(hcSite.getName());

			//获取取餐地址
			LambdaQueryWrapper<HcCompanyPickupAddress> pickupAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
			pickupAddressLambdaQueryWrapper.eq(HcCompanyPickupAddress::getCompanyId,record.getId());
			List<HcCompanyPickupAddress> companyPickupAddresses = companyPickupAddressService.list(pickupAddressLambdaQueryWrapper);
			record.setCompanyPickupAddresses(companyPickupAddresses);

			//获取企业部门信息
			LambdaQueryWrapper<HcCompanyDepartment> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
			departmentLambdaQueryWrapper.eq(HcCompanyDepartment::getCompanyId,record.getId());
			List<HcCompanyDepartment> hcCompanyDepartmentList = companyDepartmentService.list(departmentLambdaQueryWrapper);
			record.setCompanyDepartments(hcCompanyDepartmentList);

			//获取企业banner信息
			LambdaQueryWrapper<HcCompanyBanner> hcCompanyBannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
			hcCompanyBannerLambdaQueryWrapper.eq(HcCompanyBanner::getCompanyId,record.getId());
			List<HcCompanyBanner> companyBanners = companyBannerService.list(hcCompanyBannerLambdaQueryWrapper);
			record.setCompanyBanners(companyBanners);
		}
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hcCompanyInfo
	 * @return
	 */
	@AutoLog(value = "企业信息-添加")
	@ApiOperation(value="企业信息-添加", notes="企业信息-添加")
	//@RequiresPermissions("org.jeecg.modules:hc_company_info:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody HcCompanyInfo hcCompanyInfo) {
		LambdaQueryWrapper<HcCompanyInfo> hcCompanyInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
		hcCompanyInfoLambdaQueryWrapper.eq(HcCompanyInfo::getCompanyShortName,hcCompanyInfo.getCompanyShortName());
		HcCompanyInfo companyInfo = hcCompanyInfoService.getOne(hcCompanyInfoLambdaQueryWrapper);
		if (null != companyInfo){
			throw new JeecgBootException("企业简称已被使用，请更换");
		}
		hcCompanyInfoService.saveNew(hcCompanyInfo);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hcCompanyInfo
	 * @return
	 */
	@AutoLog(value = "企业信息-编辑")
	@ApiOperation(value="企业信息-编辑", notes="企业信息-编辑")
	//@RequiresPermissions("org.jeecg.modules:hc_company_info:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody HcCompanyInfo hcCompanyInfo) {
		hcCompanyInfoService.updateNew(hcCompanyInfo);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业信息-通过id删除")
	@ApiOperation(value="企业信息-通过id删除", notes="企业信息-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:hc_company_info:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		hcCompanyInfoService.removeNew(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "企业信息-批量删除")
	@ApiOperation(value="企业信息-批量删除", notes="企业信息-批量删除")
	//@RequiresPermissions("org.jeecg.modules:hc_company_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hcCompanyInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "企业信息-通过id查询")
	@ApiOperation(value="企业信息-通过id查询", notes="企业信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<HcCompanyInfo> queryById(@RequestParam(name="id",required=true) String id) {
		HcCompanyInfo hcCompanyInfo = hcCompanyInfoService.getById(id);
		if(hcCompanyInfo==null) {
			return Result.error("未找到对应数据");
		}
		//站点
		LambdaQueryWrapper<HcSite> siteLambdaQueryWrapper = new LambdaQueryWrapper<>();
		siteLambdaQueryWrapper.eq(HcSite::getId,hcCompanyInfo.getSiteId());
		HcSite hcSite = hcSiteService.getOne(siteLambdaQueryWrapper);
		hcCompanyInfo.setSiteName(hcSite.getName());

		//获取取餐地址
		LambdaQueryWrapper<HcCompanyPickupAddress> pickupAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
		pickupAddressLambdaQueryWrapper.eq(HcCompanyPickupAddress::getCompanyId,hcCompanyInfo.getId());
		List<HcCompanyPickupAddress> companyPickupAddresses = companyPickupAddressService.list(pickupAddressLambdaQueryWrapper);
		hcCompanyInfo.setCompanyPickupAddresses(companyPickupAddresses);

		//获取企业部门信息
		LambdaQueryWrapper<HcCompanyDepartment> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
		departmentLambdaQueryWrapper.eq(HcCompanyDepartment::getCompanyId,hcCompanyInfo.getId());
		List<HcCompanyDepartment> hcCompanyDepartmentList = companyDepartmentService.list(departmentLambdaQueryWrapper);
		hcCompanyInfo.setCompanyDepartments(hcCompanyDepartmentList);

		//获取企业banner信息
		LambdaQueryWrapper<HcCompanyBanner> hcCompanyBannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
		hcCompanyBannerLambdaQueryWrapper.eq(HcCompanyBanner::getCompanyId,hcCompanyInfo.getId());
		List<HcCompanyBanner> companyBanners = companyBannerService.list(hcCompanyBannerLambdaQueryWrapper);
		hcCompanyInfo.setCompanyBanners(companyBanners);
		return Result.OK(hcCompanyInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hcCompanyInfo
    */
    //@RequiresPermissions("org.jeecg.modules:hc_company_info:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HcCompanyInfo hcCompanyInfo) {
        return super.exportXls(request, hcCompanyInfo, HcCompanyInfo.class, "企业信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("hc_company_info:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HcCompanyInfo.class);
    }

}
